package com.boyunv.composite.example01;
/*
 *@description
 * 树枝节点,容器对象  可以包含叶子节点和树枝节点
 *@author boyunv
 *@create 2023/7/27 7:37
 *@version 1.0
 */

import java.util.ArrayList;

public class Composite extends Component{
 //定义集合属性,保存子节点数据
 private ArrayList<Component> list=new ArrayList<>();

 @Override
 public void add(Component c) {
   list.add(c);
 }

 @Override
 public void remove(Component c) {
   list.remove(c);
 }

 @Override
 public Component getChild(int i) {
  return list.get(i);
 }
//具体业务方法
 @Override
 public void operation() {
  //在循环中递归调用其他节点operation方法
  for (Component component : list) {
     component.operation();
  }
 }
}
